﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace Util.Forms
{
    public static class ListViewExtensions
    {
#if false
        // This function doesn't work as expected.
        public static bool Scroll(this ListView lv, int horizontalPixels, int verticalPixels)
        {
            return SendMessage_LVM_SCROLL(lv.Handle, LVM_SCROLL, horizontalPixels, verticalPixels);
        }

        [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
        static extern bool SendMessage_LVM_SCROLL(IntPtr hWnd, int Msg, int horizontal, int vertical);

        private const int LVM_FIRST = 0x1000;
        private const int LVM_SCROLL = LVM_FIRST + 20;
#endif

        /// <summary>
        /// Scrolls the given ListViewItem to the top of its containing
        /// ListView.
        /// </summary>
        /// <param name="item"></param>
        public static void ScrollToTop(this ListViewItem item)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (item.ListView == null)
                throw new InvalidOperationException("The ListViewItem is not part of a ListView.");

            item.ListView.TopItem = item;
        }

        /// <summary>
        /// Gets the bounding rectangle of the given list view subitem. This
        /// method differs from ListViewSubItem.Bounds property in that for
        /// the first sub-item of a ListViewItem, the property actually
        /// returns the bounds of the entire ListViewItem, while this method
        /// always returns only the bounds of the sub-item.
        /// </summary>
        /// <param name="subItem"></param>
        /// <returns></returns>
        public static Rectangle GetSubItemBounds(this ListViewItem item, int columnIndex)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (columnIndex < 0 || columnIndex >= item.SubItems.Count)
                throw new ArgumentOutOfRangeException("columnIndex");

            Rectangle rect = item.SubItems[columnIndex].Bounds;
            if (columnIndex == 0)
            {
                rect = new Rectangle(
                    rect.X, rect.Y, item.ListView.Columns[0].Width, rect.Height);
            }
            return rect;
        }

#if false
        public static void Activate(this ListViewItem item)
        {   
            //item.ListView.Focus();
            //if (bringToTop)
            //    lvListing.TopItem = item;
            item.ListView.TopItem = item;
            //else
            //    item.EnsureVisible();
            item.Focused = true;
            item.Selected = true;
        }
#endif
    }

#if false
    [Flags]
    enum ListViewItemActivationOptions
    {
        None = 0,
        Default = Select | Focus | EnsureVisible ,

        Select = 1,
        Focus = 2,
        FocusOwner = 4,

        EnsureVisible = 8,
        ScrollToTop = 16,
    }
#endif
}
